package com.kty.utils;

import com.kty.entity.ChineseCalendar;

// 自定义星期类
public class NongLi {

    private final String[] sWeek = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
    int iWeek;

    public NongLi() {
        iWeek = 0;
    }

    public NongLi(int w) {
        if ((w > 6) || (w < 0)) {
            System.out.println("星期超出范围，我想你想要星期天");
            this.iWeek = 0;
        } else
            this.iWeek = w;
    }

    public String toString() {
        return sWeek[iWeek];
    }
}


// 自定义日期类
class MyDate {

    public int iYear;

    public int iMonth;

    public int iDay;

    public MyDate(int iYear) {
        this.iYear = checkYear(iYear);
        this.iMonth = 1;
        this.iDay = 1;
    }

    public MyDate() {
        this.iYear = 1981;
        this.iMonth = 1;
        this.iDay = 1;
    }

    private static int checkYear(int iYear) {
        if ((iYear > 1901) && (iYear < 2050))
            return iYear;
        else {
            System.out.println("这一年超出了范围，我想你想要1981年");
            return 1981;
        }
    }

    public String toString() {
        return "" + this.iYear + (this.iMonth > 9 ? "" + this.iMonth : "0" + this.iMonth)
                + (this.iDay > 9 ? "" + this.iDay : "0" + this.iDay);
    }

    public boolean equals(MyDate md) {
        return ((md.iDay == this.iDay) && (md.iMonth == this.iMonth) && (md.iYear == this.iYear));
    }
}


// 公历日期类，继承自定义日期
class SolarDate extends MyDate {

    public SolarDate(int iYear, int iMonth, int iDay) {
        super(iYear);
        this.iMonth = checkMonth(iMonth);
        this.iDay = checkDay(this.iYear, this.iMonth, iDay);
    }

    public SolarDate(int iYear, int iMonth) {
        super(iYear);
        this.iMonth = checkMonth(iMonth);
    }

    public SolarDate(int iYear) {
        super(iYear);
    }

    public SolarDate() {
        super();
    }

    private static int checkMonth(int iMonth) {
        if (iMonth > 12) {
            System.out.println("月超出范围，我想你需要12个月");
            return 12;
        } else if (iMonth < 1) {
            System.out.println("月超出范围，我想你需要1个月");
            return 1;
        } else
            return iMonth;
    }

    private static int checkDay(int iYear, int iMonth, int iDay) {
        int iMonthDays = ChineseCalendar.iGetSYearMonthDays(iYear, iMonth);

        if (iDay > iMonthDays) {
            System.out.println("天超出了范围，我想你想要" + iMonthDays + " ");
            return iMonthDays;
        } else if (iDay < 1) {
            System.out.println("天超出了范围，我想你想要1天");
            return 1;
        } else
            return iDay;
    }

    public String toString() {
        return "" + this.iYear + (this.iMonth > 9 ? "-" + this.iMonth : "-0" + this.iMonth)
                + (this.iDay > 9 ? "-" + this.iDay : "-0" + this.iDay);
    }

}


// 农历日期类，继承自定义日期类
class LunarDate extends MyDate {

    private final String[] sChineseNum = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十"};

    public LunarDate(int iYear, int iMonth, int iDay) {
        super(iYear);
        this.iMonth = checkMonth(this.iYear, iMonth);
        this.iDay = checkDay(this.iYear, this.iMonth, iDay);
    }

    private static int checkMonth(int iYear, int iMonth) {
        if ((iMonth > 12) && (iMonth == ChineseCalendar.iGetLLeapMonth(iYear) + 12)) {
            return iMonth;
        } else if (iMonth > 12) {
            System.out.println("月超出范围，我想你需要12个月");
            return 12;
        } else if (iMonth < 1) {
            System.out.println("月超出范围，我想你需要1个月");
            return 1;
        } else
            return iMonth;
    }

    private static int checkDay(int iYear, int iMonth, int iDay) {
        int iMonthDays = ChineseCalendar.iGetLMonthDays(iYear, iMonth);

        if (iDay > iMonthDays) {
            System.out.println("天超出了范围，我想你想要" + iMonthDays + " ");
            return iMonthDays;
        } else if (iDay < 1) {
            System.out.println("天超出范围，我想你需要1天");
            return 1;
        } else
            return iDay;
    }

    public String toString() {
        String sCalendar = "农历";

        sCalendar += sChineseNum[iYear / 1000] + sChineseNum[iYear % 1000 / 100] + sChineseNum[iYear % 100 / 10]
                + sChineseNum[iYear % 10] + "(" + toChineseEra() + ")年";
        if (iMonth > 12) {
            iMonth -= 12;
            sCalendar += "闰";
        }
        if (iMonth == 12)
            sCalendar += "腊月";
        else if (iMonth == 11)
            sCalendar += "冬月";
        else if (iMonth == 1)
            sCalendar += "正月";
        else
            sCalendar += sChineseNum[iMonth] + "月";
        if (iDay > 29)
            sCalendar += "三十";
        else if (iDay > 20)
            sCalendar += "二十" + sChineseNum[iDay % 20];
        else if (iDay == 20)
            sCalendar += "二十";
        else if (iDay > 10)
            sCalendar += "十" + sChineseNum[iDay % 10];
        else
            sCalendar += "初" + sChineseNum[iDay];

        return sCalendar;
    }

    public ChineseEra toChineseEra() {
        return new ChineseEra(iYear);
    }

}


class CnWeek extends NongLi {

    private final String[] sCnWeek = {"日", "一", "二", "三", "四", "五", "六"};

    public CnWeek(int iWeek) {
        super(iWeek);
    }

    public String toString() {
        return "星期" + sCnWeek[this.iWeek];
    }
}


class ChineseEra {
    int iYear;

    String[] sHeavenlyStems = {"甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"};

    String[] sEarthlyBranches = {"子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"};

    public ChineseEra(int iYear) {
        if ((iYear < 2050) && (iYear > 1901))
            this.iYear = iYear;
        else
            this.iYear = 1981;
    }


    public String toString() {

        int temp;
        temp = Math.abs(iYear - 1924);
        return sHeavenlyStems[temp % 10] + sEarthlyBranches[temp % 12];

    }


}